package com.zh;
import com.zh.article.*;
import com.zh.creature.MonsterTiger;
import com.zh.creature.MonsterWolf;
import com.zh.creature.Person;
import com.zh.creature.PersonSet;
import com.zh.room.Room;
import com.zh.room.RoomSet;

import java.util.Scanner;

/**
 * @description:
 * @author: zhangHang
 * @date: Created in 2020/4/27 10:23
 * @version: 1.0
 * @modified By:
 */
public class Scene {
    private Person person = new Person();//初始化主角
//    private Weapon currentWeapon = new WeaponSword("pw1","罗遇剑",-120);

    private Room fog = new Room();//初始化放房间。
    private Room teaseNight = new Room();
    private Room castleInTheSky = new Room();
    private Room chaseTheWindCity = new Room();
    private Room phantomCity = new Room();
    private Room darkAbyss = new Room();
    private RoomSet roomSet = new RoomSet();

    private MonsterWolf wolf1 = new MonsterWolf();//初始化狼集合
    private MonsterWolf wolf2 = new MonsterWolf();
    private MonsterWolf wolf3 = new MonsterWolf();
    private MonsterWolf wolf4 = new MonsterWolf();
    private MonsterWolf wolf5 = new MonsterWolf();
    private MonsterWolf wolf6 = new MonsterWolf();
    private MonsterWolf wolf7 = new MonsterWolf();
    private MonsterWolf wolf8 = new MonsterWolf();
    private MonsterWolf grayWolf = new MonsterWolf();//Boss
    private MonsterTiger tiger1 = new MonsterTiger();
    private MonsterTiger tiger2 = new MonsterTiger();
    private MonsterTiger tiger3 = new MonsterTiger();
    private MonsterTiger tiger4 = new MonsterTiger();
    private MonsterTiger tiger5 = new MonsterTiger();

    private Person person2 = new Person();//初始化帮手集合
    private Person person3 = new Person();
    private PersonSet personSet = new PersonSet();

    public void initScene(){
            initRoomSet();//必须先初始化房间
            initPerson();//先初始化主角再怪兽
            initMonster();
            initPersonSet();

    }

    public void initRoomSet(){
        fog.setDescription("雾都");
        fog.setId("fog");
        fog.addReachableRoom("teaseNight",teaseNight);
        fog.addReachableRoom("phantomCity",phantomCity);
        fog.addReachableRoom("chaseTheWindCity",chaseTheWindCity);

        teaseNight.setDescription("撩夜城");
        teaseNight.setId("teaseNight");
        teaseNight.addReachableRoom("fog",fog);
        teaseNight.addReachableRoom("chaseTheWindCity",chaseTheWindCity);
        teaseNight.addReachableRoom("castleInTheSky",castleInTheSky);
        teaseNight.getWeaponSet().getWeapons().add(new WeaponSword("rpw1","屠龙剑",-16));
        teaseNight.getWeaponSet().getWeapons().add(new WeaponSword("rpw2","轻语剑",-40));
        teaseNight.getDrugSet().getDrugs().add(new DrugTreatment("rpd1","七仙丹",40));

        castleInTheSky.setDescription("天空之城");
        castleInTheSky.setId("castleInTheSky");
        castleInTheSky.addReachableRoom("chaseTheWindCity",chaseTheWindCity);
        castleInTheSky.getDrugSet().getDrugs().add(new DrugTreatment("rpd4","肉丸子",45));

        phantomCity.setDescription("幻影城");
        phantomCity.setId("phantomCity");
        phantomCity.addReachableRoom("chaseTheWindCity",chaseTheWindCity);
        phantomCity.getWeaponSet().getWeapons().add(new WeaponSword("rpw3","浪花王剑",-520));
        phantomCity.getWeaponSet().getWeapons().add(new WeaponSword("rpw4","心雨剑",-168));

        chaseTheWindCity.setDescription("追风城");
        chaseTheWindCity.setId("chaseTheWindCity");
        chaseTheWindCity.addReachableRoom("teaseNight",teaseNight);
        chaseTheWindCity.getDrugSet().getDrugs().add(new DrugIntensify("rpd2","火焰丹",50));//加攻击力的
        chaseTheWindCity.getDrugSet().getDrugs().add(new DrugTreatment("rpd3","伸腿瞪眼丸",66));

        roomSet.addRoom(fog);
        roomSet.addRoom(teaseNight);
        roomSet.addRoom(castleInTheSky);
        roomSet.addRoom(phantomCity);
        roomSet.addRoom(chaseTheWindCity);
    }//地图,和地图里面的武器库，药品库

    public void initPerson(){
//            person.setId("201902225");
//            person.setDescription("小张");

            person.setId("protagonist");
            person.setHpValue(300);
            person.setCurrentWeapon(new WeaponSword("pw1","罗遇剑",-120));
            person.getWeaponSet().getWeapons().add(person.getCurrentWeapon());//增加到武器库。
            person.setCurrentRoom(teaseNight);
            person.getDrugSet().getDrugs().add(new DrugTreatment("drug","起死回生丹",500));//初始化药品库
            person.setSpecialSkills(new Teleport("sk1","瞬间移动"));
    }//主角

    public void initMonster(){
            wolf1.setId("wolf1");
            wolf1.setDescription("狼1");
            wolf1.setHpValue(40);
            wolf1.setCurrentWeapon(new WeaponKnife("mw1","截天刀",-30));

            wolf2.setId("wolf2");
            wolf2.setDescription("狼2");
            wolf2.setHpValue(60);
            wolf2.setCurrentWeapon(new WeaponKnife("mw2","凤羽刀",-20));

            wolf3.setId("wolf3");
            wolf3.setDescription("狼3");
            wolf3.setHpValue(20);
            wolf3.setCurrentWeapon(new WeaponKnife("mw3","雨落刀",-49));

            wolf4.setId("wolf4");
            wolf4.setDescription("狼四");
            wolf4.setHpValue(70);
            wolf4.setCurrentWeapon(new WeaponKnife("mw4","蓝山刀",-60));

            wolf5.setId("wolf5");
            wolf5.setDescription("狼五");
            wolf5.setHpValue(10);
            wolf5.setCurrentWeapon(new WeaponKnife("mw5","天山刀",-15));

            wolf6.setId("wolf6");
            wolf6.setDescription("狼六");
            wolf6.setHpValue(45);
            wolf6.setCurrentWeapon(new WeaponKnife("mw6","火焰刀",-18));

            wolf7.setId("wolf7");
            wolf7.setDescription("狼七");
            wolf7.setHpValue(5);
            wolf7.setCurrentWeapon(new WeaponKnife("mw7","雪花刀",-30));

            wolf8.setId("wolf8");
            wolf8.setDescription("狼八");
            wolf8.setHpValue(13);
            wolf8.setCurrentWeapon(new WeaponKnife("mw8","御道刀",-3));

            grayWolf.setId("grayWolf");
            grayWolf.setDescription("灰太狼");
            grayWolf.setHpValue(500);
            grayWolf.setCurrentWeapon(new WeaponKnife("mw9","霸王刀",-100));

            tiger1.setId("tiger1");
            tiger1.setDescription("虎1");
            tiger1.setHpValue(200);
            tiger1.setCurrentWeapon(new WeaponKnife("tw1","虎刀",-49));

            tiger2.setId("tiger2");
            tiger2.setDescription("虎2");
            tiger2.setHpValue(120);
            tiger2.setCurrentWeapon(new WeaponKnife("tw2","老虎刀",-55));

            tiger3.setId("tiger3");
            tiger3.setDescription("虎3");
            tiger3.setHpValue(45);
            tiger3.setCurrentWeapon(new WeaponKnife("tw3","映雪刀",-989));

            tiger4.setId("tiger4");
            tiger4.setDescription("虎4");
            tiger4.setHpValue(123);
            tiger4.setCurrentWeapon(new WeaponKnife("tw4","毛刀",-45));

            tiger5.setId("tiger5");
            tiger5.setDescription("虎5");
            tiger5.setHpValue(100);
            tiger5.setCurrentWeapon(new WeaponKnife("tw5","狂风刀",-99));


            roomSet.searchRoomById("fog").getMonsterWolfSet().getMonsterWolves().add(wolf1);//为某个房间设置怪兽。
            roomSet.searchRoomById("fog").getMonsterWolfSet().getMonsterWolves().add(wolf2);
            roomSet.searchRoomById("fog").getMonsterWolfSet().getMonsterWolves().add(wolf3);

            roomSet.searchRoomById("chaseTheWindCity").getMonsterWolfSet().getMonsterWolves().add(wolf4);
            roomSet.searchRoomById("chaseTheWindCity").getMonsterWolfSet().getMonsterWolves().add(wolf5);
            roomSet.searchRoomById("chaseTheWindCity").getMonsterTigerSet().getMonsterTigers().add(tiger4);

            roomSet.searchRoomById("phantomCity").getMonsterWolfSet().getMonsterWolves().add(wolf6);
            roomSet.searchRoomById("phantomCity").getMonsterWolfSet().getMonsterWolves().add(wolf7);

            roomSet.searchRoomById("castleInTheSky").getMonsterWolfSet().getMonsterWolves().add(wolf8);
            roomSet.searchRoomById("castleInTheSky").getMonsterTigerSet().getMonsterTigers().add(tiger3);

            roomSet.searchRoomById("teaseNight").getMonsterTigerSet().getMonsterTigers().add(tiger1);
            roomSet.searchRoomById("teaseNight").getMonsterTigerSet().getMonsterTigers().add(tiger2);


    }//怪兽

    public void initPersonSet(){
        person2.setId("person2");
        person2.setHpValue(100);
        person2.setDescription("衫哥");
        person2.setCurrentRoom(teaseNight);
        person2.setCurrentWeapon(new WeaponDagger("pw2","水起匕",-65));
        person3.setId("person3");
        person3.setDescription("花哥");
        person3.setHpValue(80);
        person3.setCurrentRoom(teaseNight);
        person3.setCurrentWeapon(new WeaponKnife("pw3","万月刀",-45));

        personSet.getPeople().add(person2);
        personSet.getPeople().add(person3);
    }//帮手

    //打开终局之战的接口
    public void initFinalBattle(){
        darkAbyss.setDescription("黑暗深渊");
        darkAbyss.setId("darkAbyss");
        this.chaseTheWindCity.addReachableRoom("darkAbyss",darkAbyss);
        roomSet.addRoom(darkAbyss);
        roomSet.searchRoomById("darkAbyss").getMonsterWolfSet().getMonsterWolves().add(this.grayWolf);
        roomSet.searchRoomById("darkAbyss").getMonsterTigerSet().getMonsterTigers().add(this.tiger5);

    }

    public void play(){
        System.out.println("Please initialization(input description.)");
        Scanner in = new Scanner(System.in);
        String initial = in.nextLine();
        String[] initialization=initial.split(" ");
        person.setDescription(initialization[0]);
        Ui.displayStatus(this);//输出基本信息,己方，敌方
        Ui.displayWeapons(this.getPerson());//场景中一个房间的武器库
        Ui.displayDrugs(this.getPerson());//场景中一个房间的药品库
        while (true){
            String command = in.nextLine();
            String[] cmdLineItems = command.split(" ");//空格必须打起，不然不管输入多少字符都没有效果。
            if(cmdLineItems[0].equals("bye")){
                Ui.displayBye();
                System.exit(0);
            }else if(cmdLineItems[0].equals("help")){
                Ui.displayHelpMsg();
            }else if(cmdLineItems[0].equals("chopW")){

                //其他帮手的攻击，帮手听从主角的攻击。帮手先上
                for (Person item: this.getPersonSet().getPeople()
                ) {
                    //如果攻击一个不存在在生物,因为己方已经发动了攻击，所以敌方也可以发动攻击。（一个逻辑）
                    if(item.getCurrentRoom().getMonsterWolfSet().get(cmdLineItems[1])!=null){//判断狼是否已经被杀死,
                        Ui.displayDamageMsg(item,item.getCurrentRoom().getMonsterWolfSet().get(cmdLineItems[1]));//先输出伤害
                        item.useWeapon(item.getCurrentWeapon(),item.getCurrentRoom().getMonsterWolfSet().get(cmdLineItems[1]),this);
                    }
                }

                //主角的攻击
                if(this.getPerson().getCurrentRoom().getMonsterWolfSet().get(cmdLineItems[1])!=null){//判断狼是否已经被杀死
                    Ui.displayDamageMsg(this.getPerson(),this.getPerson().getCurrentRoom().getMonsterWolfSet().get(cmdLineItems[1]));//先输出伤害
                    this.getPerson().useWeapon(this.getPerson().getCurrentWeapon(),
                            this.getPerson().getCurrentRoom().getMonsterWolfSet().get(cmdLineItems[1]),this);
                }

                //怪兽的攻击,之前死的就没有办法攻击了，所以主角占优势。
                for (MonsterWolf item: this.getPerson().getCurrentRoom().getMonsterWolfSet().getMonsterWolves()
                     ) {
                    if(1==Utils.randomMonsterChop()){//会存在既不攻击主角和帮手的情况
                        Ui.displayDamageMsg(item,this.getPerson());//先输出伤害
                        item.useWeapon(item.getCurrentWeapon(),this.getPerson(),this);
                    }
                    else if(this.getPersonSet().getPeople().size()!=0){
                            Ui.displayDamageMsg(item,this.getPersonSet().getPeople().get(0));//先输出伤害，并且按照顺序先打第一个帮手
                            item.useWeapon(item.getCurrentWeapon(),this.getPersonSet().getPeople().get(0),this);
                    }
                }
            }else if(cmdLineItems[0].equals("chopT")){

                for (Person item: this.getPersonSet().getPeople()
                ) {
                    if(item.getCurrentRoom().getMonsterTigerSet().get(cmdLineItems[1])!=null){//判断老虎是否已经被杀死
                        Ui.displayDamageMsg(item,item.getCurrentRoom().getMonsterTigerSet().get(cmdLineItems[1]));//先输出伤害
                        item.useWeapon(item.getCurrentWeapon(),item.getCurrentRoom().getMonsterTigerSet().get(cmdLineItems[1]),this);
                    }
                }
                //主角的攻击
                if(this.getPerson().getCurrentRoom().getMonsterTigerSet().get(cmdLineItems[1])!=null){
                    Ui.displayDamageMsg(this.getPerson(),this.getPerson().getCurrentRoom().getMonsterTigerSet().get(cmdLineItems[1]));//先输出伤害
                    this.getPerson().useWeapon(this.getPerson().getCurrentWeapon(),
                            this.getPerson().getCurrentRoom().getMonsterTigerSet().get(cmdLineItems[1]),this);
                }

                //怪兽的攻击,之前死的就没有办法攻击了，所以主角占优势。
                for (MonsterTiger item: this.getPerson().getCurrentRoom().getMonsterTigerSet().getMonsterTigers()
                ) {
                    if(1==Utils.randomMonsterChop()){
                        Ui.displayDamageMsg(item,this.getPerson());//先输出伤害
                        item.useWeapon(item.getCurrentWeapon(),this.getPerson(),this);
                    }
                    else if(this.getPersonSet().getPeople().size()!=0){
                        Ui.displayDamageMsg(item,this.getPersonSet().getPeople().get(0));//先输出伤害，并且按照顺序先打第一个帮手
                        item.useWeapon(item.getCurrentWeapon(),this.getPersonSet().getPeople().get(0),this);
                    }
                }
            } else if(cmdLineItems[0].equals("go")){
                this.getPerson().go(cmdLineItems[1]);//主角走
//                Ui.displayReachableRooms(this.getPerson());
                for (Person item:this.getPersonSet().getPeople()//帮手走
                     ) {
                    item.go(cmdLineItems[1]);
                }
            }else if(cmdLineItems[0].equals("pickW")){
                this.getPerson().pickWeapon(cmdLineItems[1]);
                this.getPerson().getCurrentRoom().removeWeapon(cmdLineItems[1]);
            }else if(cmdLineItems[0].equals("pickD")){
                this.getPerson().pickDrug(cmdLineItems[1]);
                this.getPerson().getCurrentRoom().removeDrug(cmdLineItems[1]);
            }else if(cmdLineItems[0].equals("useD")){
                this.getPerson().useDrug(cmdLineItems[1]);
                this.getPerson().getDrugSet().remove(cmdLineItems[1]);
            } else if(cmdLineItems[0].equals("equip")){
                this.getPerson().changeWeapon(cmdLineItems[1]);
            }else if(cmdLineItems[0].equals("useS")){
                this.getPerson().getSpecialSkills().useSpecialSkill(this.getPerson(),this.getPersonSet(),cmdLineItems[1],this);
            } else {
                Ui.displayErrorCmdMsg();
                Ui.displayHelpMsg();
            }
            Ui.displayStatus(this);//再次输出基本信息
            Ui.displayWeapons(this.getPerson());
            Ui.displayDrugs(this.getPerson());

        }

    }

    public boolean judgeSuccess(){
        for (Room item:this.roomSet.getRooms()
             ) {
            if((item.getMonsterWolfSet().getMonsterWolves().size()!=0)||item.getMonsterTigerSet().getMonsterTigers().size()!=0){
                return false;//说明还有怪兽，用来判断是否进入终局之战。
            }
        }
        return true;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public PersonSet getPersonSet() {
        return personSet;
    }

    public RoomSet getRoomSet() {
        return roomSet;
    }

    public void setRoomSet(RoomSet roomSet) {
        this.roomSet = roomSet;
    }

    public MonsterWolf getGrayWolf() {
        return grayWolf;
    }

    public void setGrayWolf(MonsterWolf grayWolf) {
        this.grayWolf = grayWolf;
    }
}
